
include mod_interf.inc
include mod_loader.inc
include mod_memb.inc
include mod_bin_interf_creator.inc


MOD_FL_NEEDS_CALL_TO_READ	EQU 1
MOD_FL_NEEDS_CALL_TO_WRITE	EQU 2
MOD_FL_NEEDS_CALL_TO_CALL	EQU 4
MOD_FL_LUA			EQU 8

;----------------------------------------------------
_@module	struc
	module_flags		dd 0
	lpszModuleName		dd 0
	lpszModuleDoc		dd 0
	;---------------
	lpRequiredFirstModule	dd 0
	;---------------
	lpFirstMember		dd 0
	;---------------
;	pCallback_toRead	dd 0
	lpCallback_toRead_first_fixe	dd 0
	lParam_r		dd 0 ;if func-type
	offset_from_begin_r	dd 0
	;---
;	lpCallback_toWrite	dd 0
	lpCallback_toWrite_first_fixe	dd 0
	lParam_w		dd 0 ;if func-type
	offset_from_begin_w	dd 0
	;---
;	lpCallback_toCall	dd 0
	lpCallback_toCall_first_fixe	dd 0
	lParam_c		dd 0 ;if func-type
	offset_from_begin_c	dd 0
	;---------------
	offset_from_begin	dd 0
	;---------------
	lpFirstOriginFixe	dd 0
	lpFirstOffsetToInterfVarFixe dd 0
	;---------------
	lpNextModule		dd 0
_@module	ends

;----------------------------------------------------

.data
szLua	db "lua",0

;moduleRoot	dd 0
;module_num	dd 0
;----------------------------------------------------


.code

extrn get_mod_var@12:near	;(lua_State *L,char *var,int type);
get_mod_var	equ get_mod_var@12
extrn set_mod_var@12:near	;(lua_State *L,char *var,void *value);
set_mod_var	equ set_mod_var@12
extrn call_mod_func@12:near	;(lua_State *L,char *func);
call_mod_func	equ call_mod_func@12

extrn current_module:near

module_check	proc lpCurModule,lpszModName
			push esi
			push edi
			push ecx

			mov eax,lpszModName
			test eax,eax
			jz  mcExit;short
			mov esi,eax

;			mov edi,dword ptr [current_module] ;offset moduleRoot

		assume edi: ptr _@module
			mov edi,current_module
			mov eax,lpCurModule
			test eax,eax
			jz short @f
		assume eax: ptr _@module
			lea edi,[eax].lpRequiredFirstModule
		@@:

	mcModSeek_b:
			mov eax,dword ptr [edi]
			test eax,eax
			jz short mcModSeek_add_module

				push eax

			push esi
			push [eax].lpszModuleName
			call strcmp
;			add esp,4*2

			test eax,eax
				pop eax
			jz  mcModSeek_e;short

			lea edi,[eax].lpNextModule
			jmp short mcModSeek_b
	
	mcModSeek_add_module:
;			mov edi,offset moduleRoot
;			push eax
;			
;			mov eax,lpCurModule
;			test eax,eax
;			jz short @f
;		assume eax: ptr _@module
;			lea edi,[eax].lpRequiredFirstModule
;		@@:
;			pop eax

		_@getMem sizeof _@module
			test eax,eax
			jz  mcExit;short

			push dword ptr [edi]
			mov dword ptr [edi],eax
			pop [eax].lpNextModule

			mov edi,eax

			push esi
			call CloneString
			mov [edi].lpszModuleName,eax

	mcSeeksTheRealModule:

	mcIsRequiredModule_Lua:
			push esi ;[edi].lpszModuleName
			push offset szLua
			call strcmp
;			add esp,4*2

			test eax,eax
			jnz short mgRequiredModuleIs_NotLua


			mov [edi].module_flags,MOD_FL_LUA or MOD_FL_NEEDS_CALL_TO_READ or MOD_FL_NEEDS_CALL_TO_WRITE or MOD_FL_NEEDS_CALL_TO_CALL
;			mov [edi].lpCallback_toRead, get_mod_var
;			mov [edi].lpCallback_toWrite, set_mod_var
;			mov [edi].lpCallback_toCall, call_mod_func

			mov eax,edi
			jmp  mcModSeek_e;short

	mgRequiredModuleIs_NotLua:
;push esi
;call print

			push esi
			call get_bin_module_interf
			test eax,eax
			jnz short @f
	mcNoModule:
			mov eax,lpCurModule
			test eax,eax
			mov eax,edi
			jz  mcExit;short
		_@freeMem edi
			xor eax,eax
			jmp short mcExit
		@@:
			mov esi,[eax+_@module_link.lpModule]

			push esi
			call IsThisModule
			test eax,eax
			jz short mcNoModule

			add esi,dword ptr [esi+2]
			add esi,MOD_INTERF_CREATING_OFFSET
		_@get_bin_mod_len	;interf len
		_@get_bin_mod_len	;main mod interf len
			mov ecx,eax
			add ecx,esi	;ecx=mod end point
		_@get_bin_mod_len	;main mod name len
			add esi,eax
		_@get_bin_mod_len	;main mod doc len
			add esi,eax
		_@get_bin_mod_len	;read func len
			add esi,eax
			test eax,eax
			jz short @f
			or [edi].module_flags,MOD_FL_NEEDS_CALL_TO_READ
		@@:
		_@get_bin_mod_len	;write func len
			add esi,eax
			test eax,eax
			jz short @f
			or [edi].module_flags,MOD_FL_NEEDS_CALL_TO_WRITE
		@@:
		_@get_bin_mod_len	;call func func len
			add esi,eax
			test eax,eax
			jz short @f
			or [edi].module_flags,MOD_FL_NEEDS_CALL_TO_CALL
		@@:

;		push edi
;
;			cmp esi,ecx
;			jnc short mcAfterModMembCreation
;	;create the mod members
;			push ecx
;		_@getMem sizeof _@module_member
;			pop ecx
;
;			mov edi,eax
;		assume edi: ptr _@module_member
;
;			
;
;
;		mcAfterModMembCreation:
;
;		pop edi

			mov eax,edi
	mcModSeek_e:

	mcExit:
			pop ecx
			pop edi
			pop esi
			ret
module_check		endp
;----------------------------------------------------
module_get		proc lpCurModule,lpszModName
			push esi
			push edi

			mov eax,lpCurModule
;			test eax,eax
;			jz short mgExit
			mov edi,eax
		assume edi: ptr _@module

			mov eax,lpszModName
			test eax,eax
			jz short mgExit
			mov esi,eax

			push esi
			push edi
			call module_check
			push eax

		_@freeMem esi

			pop eax
;			test eax,eax
;			jz short mgExit
;			mov edi,eax
;		assume edi: ptr _@module
;
;			mov eax,edi
	mgExit:
			pop edi
			pop esi
			ret
module_get		endp
;----------------------------------------------------
; on return:
;	ecx = type (0-address/1-func),
;	ebx = lParam (if type = func - passed as first param)
;	eax = lpModuleMember
module_member_get proc lpModule,lpszModMemName,access_type
			push esi
			push edi

			mov eax,lpszModMemName
			test eax,eax
			jz  mmgExit;short
			mov esi,eax

			mov eax,lpModule
			test eax,eax
			jz short mmgExit;
			mov edi,eax
		assume edi: ptr _@module

;			mov eax,moduleRoot
;		assume eax: ptr _@module
;	mmgModCheck_b:
;			test eax,eax
;			jz short mmgExit
;
;			cmp eax,edi
;			jz short mmgModCheck_e
;
;			mov eax,[eax].lpNextModule
;	mmgModCheck_e:

;printfX 01f1h
			push access_type
			push esi ;lpszModMemName
			push edi ;lpModule
			call module_memb_get
			push eax

			test esi,esi
			jz short @f
		_@freeMem esi
		@@:

			pop eax
			test eax,eax
			jz short mmgExit
;printfX 01f2h

			push eax

			mov eax,access_type
			xor ebx,ebx
			mov ecx,MOD_MEMB_RET_TYPE_ADDRESS
	mmgIsAccess_read:
			dec eax
			jnz short mmgIsAccess_write

			test [edi].module_flags,MOD_FL_NEEDS_CALL_TO_READ
			jz short mmgAfterResultPreparing

			mov ecx,MOD_MEMB_RET_TYPE_FUNC
			mov ebx,[edi].lParam_r
;			mov eax,[edi].lpCallback_toRead
			jmp short mmgAfterResultPreparing
		@@:

	mmgIsAccess_write:
			dec eax
			jnz short mmgIsAccess_call

			test [edi].module_flags,MOD_FL_NEEDS_CALL_TO_WRITE
			jz short mmgAfterResultPreparing

			mov ecx,MOD_MEMB_RET_TYPE_FUNC
			mov ebx,[edi].lParam_w
;			mov eax,[edi].lpCallback_toWrite
			jmp short mmgAfterResultPreparing
		@@:

		
	mmgIsAccess_call:
			dec eax
			jnz short mmgIsAccess_???

			test [edi].module_flags,MOD_FL_NEEDS_CALL_TO_CALL
			jz short mmgAfterResultPreparing

			mov ecx,MOD_MEMB_RET_TYPE_FUNC
			mov ebx,[edi].lParam_c
;			mov eax,[edi].lpCallback_toCall
			jmp short mmgAfterResultPreparing
		@@:

		
	mmgIsAccess_???:
			pop eax
			xor eax,eax
			jmp short mmgExit

	mmgAfterResultPreparing:
			pop eax
	mmgExit:
;			mov edi,eax
;
;			test esi,esi
;			jz short @f
;		_@freeMem esi
;		@@:
;			mov eax,edi

			pop edi
			pop esi
			ret
module_member_get	endp
;----------------------------------------------------
module_member_new proc lpModule,lpszModMemName,lpszMemb_type,offs,access_type
			push esi
			push edi

			mov eax,lpszModMemName
			test eax,eax
			jz  mmnExit;short
			mov esi,eax

			mov eax,lpModule
			test eax,eax
			jz short mmnExit;
			mov edi,eax
		assume edi: ptr _@module

			mov eax,[edi].lpFirstMember
	mmnCheckMember_b:
		assume eax: ptr _@module_member
			test eax,eax
			jz short mmnCheckMember_e

				push eax

			push [eax].lpszModMembName
			push esi
			call strcmp
			test eax,eax
				pop eax
			jz short mmnSetOffs

			mov eax,[eax].lpNextModuleMember
			jmp short mmnCheckMember_b
	mmnCheckMember_e:

		_@getMem sizeof _@module_member
			test eax,eax
			jz short mmnExit
				push esi
				push esi
			mov esi,eax
		assume esi: ptr _@module_member

			mov eax,[edi].lpFirstMember
			mov [edi].lpFirstMember,esi
			mov [esi].lpNextModuleMember,eax

			mov [esi].lpModule,edi

				call CloneString
			mov [esi].lpszModMembName,eax
				pop eax
		_@freeMem eax

			push lpszMemb_type
			push esi
			call module_memb_set_type
;			call module_memb_convert_type
;			mov [esi].memb_type,eax

			mov eax,esi

	mmnSetOffs:
			mov esi,eax
			mov eax,offs
			mov [esi].lpAddress,eax

			mov eax,esi

	mmnExit:
			pop edi
			pop esi
			ret
module_member_new	endp
;----------------------------------------------------
module_access_func_set_fixe	proc lpModule,fixe,access_type,id
			push edi
			push ebx
			push ecx
			push edx

			mov eax,lpModule
			test eax,eax
			jz short mmsfExit
			mov edi,eax
		assume edi: ptr _@module

			xor eax,eax
			mov ebx,access_type
	mafsfIsAccess_read:
			dec ebx
			jnz short mafsfIsAccess_write

			test [edi].module_flags,MOD_FL_NEEDS_CALL_TO_READ
			jz short mmsfExit

			lea edi,[edi].lpCallback_toRead_first_fixe
			jmp short mafsfSetsTheFixe

	mafsfIsAccess_write:
			dec ebx
			jnz short mafsfIsAccess_call

			test [edi].module_flags,MOD_FL_NEEDS_CALL_TO_WRITE
			jz short mmsfExit

			lea edi,[edi].lpCallback_toWrite_first_fixe
			jmp short mafsfSetsTheFixe

	mafsfIsAccess_call:
			dec ebx
			jnz short mafsfIsAccess_???

			test [edi].module_flags,MOD_FL_NEEDS_CALL_TO_CALL
			jz short mmsfExit

			lea edi,[edi].lpCallback_toCall_first_fixe
			jmp short mafsfSetsTheFixe

	mafsfIsAccess_???:
 			jmp short mmsfExit

	mafsfSetsTheFixe:
			mov eax,dword ptr [edi]
		assume eax: ptr _@module_fixe
			mov ebx,id
		@@:
			test eax,eax
			jz short @f
			cmp [eax].id,ebx
			jz short mafsfStoreFixe
			mov eax,[eax].lpNextFixe
			jmp short @b
		@@:



		_@getMem sizeof _@module_fixe
			test eax,eax
			jz short mmsfExit
		assume eax: ptr _@module_fixe

			mov ebx,id
			mov [eax].id,ebx

			mov ebx,dword ptr [edi]
			mov dword ptr [edi],eax
			mov [eax].lpNextFixe,ebx

	mafsfStoreFixe:
			mov ebx,fixe
			mov [eax].addr_to_fixe,ebx

	mmsfExit:
			xor eax,eax	;??
			pop edx
			pop ecx
			pop ebx
			pop edi
			ret
 module_access_func_set_fixe	endp
;----------------------------------------------------
module_origin_quest_set_fixe	proc lpModule,fixe,id
			push edi
			push ebx
			push ecx
			push edx

			mov eax,lpModule
			test eax,eax
			jz short mmsfExit
			mov edi,eax
		assume edi: ptr _@module

			mov eax,[edi].lpFirstOriginFixe
		assume eax: ptr _@module_fixe
			mov ebx,id
		@@:
			test eax,eax
			jz short @f
			cmp [eax].id,ebx
			jz short moqsfStoreFixe
			mov eax,[eax].lpNextFixe
			jmp short @b
		@@:

			lea edi,[edi].lpFirstOriginFixe
	mafsfSetsTheFixe:
		_@getMem sizeof _@module_fixe
			test eax,eax
			jz short mmsfExit
		assume eax: ptr _@module_fixe

			push id
			pop [eax].id

			push dword ptr [edi]
			mov dword ptr [edi],eax
			pop [eax].lpNextFixe

	moqsfStoreFixe:
			push fixe
			pop [eax].addr_to_fixe

	mmsfExit:
			pop edx
			pop ecx
			pop ebx
			pop edi
			ret
 module_origin_quest_set_fixe	endp
;----------------------------------------------------
;----------------------------------------------------
;----------------------------------------------------
;----------------------------------------------------
;----------------------------------------------------
;----------------------------------------------------
;----------------------------------------------------
;----------------------------------------------------
;----------------------------------------------------
;----------------------------------------------------

